package com.android.tianyuanyouxuan.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.android.tianyuanyouxuan.bean.MyCustomTabEntity;
import com.android.tianyuanyouxuan.bean.News;
import com.android.tianyuanyouxuan.databinding.FragmentXuetangBinding;
import com.android.tianyuanyouxuan.presenter.XueTangPresenter;
import com.android.tianyuanyouxuan.ui.activity.NewsDetailsActivity;
import com.android.tianyuanyouxuan.ui.activity.SendNewsActivity;
import com.android.tianyuanyouxuan.ui.adapter.XueTangAdapter;
import com.blankj.utilcode.util.ActivityUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flyco.tablayout.listener.CustomTabEntity;

import java.util.ArrayList;

public class XuetangFragment extends Fragment {

    private FragmentXuetangBinding viewBinding;

    private XueTangAdapter adapter = new XueTangAdapter();

    private XueTangPresenter presenter = new XueTangPresenter();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewBinding = FragmentXuetangBinding.inflate(LayoutInflater.from(requireContext()));
        return viewBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ArrayList<CustomTabEntity> tabEntitys = new ArrayList<>();
        tabEntitys.add(new MyCustomTabEntity("新闻"));
        tabEntitys.add(new MyCustomTabEntity("咨询"));
        viewBinding.tlType.setTabData(tabEntitys);
        viewBinding.rvList.setLayoutManager(new LinearLayoutManager(requireContext()));
        viewBinding.rvList.setAdapter(adapter);

        viewBinding.btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtils.startActivity(SendNewsActivity.class);
            }
        });

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
                Intent intent = new Intent(requireContext(), NewsDetailsActivity.class);
                News news = adapter.getItem(position);
                intent.putExtra("data", news.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        adapter.setNewInstance(presenter.getNewsList());
    }
}
